import React from 'react';
import {
    RefreshControl,
    AsyncStorage,
    Platform,
    Dimensions,
    Keyboard
} from 'react-native';
import NetUtil from "./NetUtil.js"

var Tools = {
    isDataValid: function (data="") {
        if (data != null&&data!=undefined) {
            data=data.toString()
            if(data != "" && data != "undefined"&&data!="null"){
               return true; 
            }  
        }
        return false;    
    },
    postBase64:function(url, data, successCallBaack, errCallBack){
        data = JSON.stringify(data);
        data = this.base64encode(data);
        return NetUtil.request(url,encodeURIComponent(data),successCallBaack, errCallBack)
    },
    post: function (url, data, successCallBaack, errCallBack) {
        return NetUtil.request(url,JSON.stringify(data),successCallBaack, errCallBack)
       
    },
    postForm:function(url, data, successCallBaack, errCallBack) {
        return NetUtil.requestForm(url,data,successCallBaack, errCallBack)
    },
    get: function (url, successCallBaack, errCallBack) {
        console.log("========",url)
        return NetUtil.request(url,null,successCallBaack, errCallBack)
    },
    /**
     * scrollview 下拉刷新控制器
     * @param isRefreshing
     * @param _onRefresh
     * @returns {XML}
     */
    intiRefresh: function (isRefreshing, _onRefresh) {
        return <RefreshControl style={Platform.OS=='ios'?{left:60}:{}}
            refreshing={isRefreshing}
            onRefresh={_onRefresh}
            tintColor="#ff5400"
            title="刷新中"
            colors={['#ff5400','#0db789']}/>
    },

    checkPhone:function(pPhone){
        if(!this.isDataValid(pPhone)){
            return("请输入手机号码");
        }
        pPhone=pPhone.replace(" ","");
        if (!this.isDataValid(pPhone)||pPhone<=0) {
            return("手机号码不能为空!");
        } else{
            var phone=pPhone
            if(pPhone.indexOf("+86")>=0){
                phone =pPhone.split("+86")[1];
            }
            if (phone.length != 11) {
                return("请输入11位手机号码!");
            }
            // else if(!(/^1[1-9][0-9]\d{8}$/.test(phone))){
            //     return("手机号码格式不正确");
            // }
             else {
                return null;
            }
        }       
    },

    /**
    *验证token是否是游客
    */
    isTokenLogin:function(token){
        if(this.isDataValid(token)&&token!='guest'){
            return true;
        }else{
            return false;
        }
    },
    /**
    *不需要强制登录的情况下获取token，已经登录了则返回登录的token，否则返回guest 表示游客登录
    */
    getToken:function(callback){
        this.getStorage("token",(value)=>{
            // alert(value);
            if (this.isDataValid(value)) {
                callback(value);
            } else {
                callback("guest")
            }
        });
    },
    //判断订单系统、库存报表模块的token是否过期
    isTokenExpire(time){
        try{
            if(Tools.isDataValid(time)&&time.indexOf(" ")>0){
                return !time.split(" ")[0]>new Date().Format("yyyy-MM-dd")
            }
            return true;
        }catch(e){
            return true
        }
    },
    getStorage:function(key, callback){
        AsyncStorage.getItem(key.toLowerCase())
            .then((value) => {
                callback(value);
            })
            .catch((error) => {
                console.log("----get token err----" + error);
                // alert(error)
                callback(null)
            })
            .done();
    },
    setStorage:function(key, value){
        AsyncStorage.setItem(key.toLowerCase(), value)
            .then(()=> {
            })
            .catch((error) => {

            })
            .done();
    },
    removeStorage:function(key){
        AsyncStorage.removeItem(key.toLowerCase())
            .then(()=> {
            })
            .catch((error) => {

            })
            .done();
    },
    /**
     * base64编码
     * @param str
     * @returns {string}
     */
    base64encode:function(str) {
        var out, i, len, base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        var c1, c2, c3;
        len = str.length;
        i = 0;
        out = "";
        while (i < len) {
            c1 = str.charCodeAt(i++) & 0xff;
            if (i == len) {
                out += base64EncodeChars.charAt(c1 >> 2);
                out += base64EncodeChars.charAt((c1 & 0x3) << 4);
                out += "==";
                break;
            }
            c2 = str.charCodeAt(i++);
            if (i == len) {
                out += base64EncodeChars.charAt(c1 >> 2);
                out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
                out += base64EncodeChars.charAt((c2 & 0xF) << 2);
                out += "=";
                break;
            }
            c3 = str.charCodeAt(i++);
            out += base64EncodeChars.charAt(c1 >> 2);
            out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
            out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
            out += base64EncodeChars.charAt(c3 & 0x3F);
        }
        return out;
    },


};
module.exports = Tools;
